package Init;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import Log.LogManager;

public class InitDatabase extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public InitDatabase() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>重新初始化</TITLE></HEAD>");
		out.println("  <BODY>");
		out.println("调用这个Servlet可以进行初始化，OnlineJudge系统初始化完毕！");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
		doPost(request, response);
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		init();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
		LogManager.log("这是JOJ评测系统OnlineJudge线上壳，请先启动JudgeCore核心再起动本系统！");
		LogManager.log("服务器初始化成功！");
		LogManager.log("开始检查数据点设置！");
		ServletContext application=getServletContext();
		String dburl=getInitParameter("dburl")+"?useUnicode=true&characterEncoding=utf8";
		String username=getInitParameter("username");
		String password=getInitParameter("password");
		String thishost=getInitParameter("thishost");
		String realpath=getInitParameter("realpath");
		String datapath=getInitParameter("datapath");
		String systempi=getInitParameter("systempi");
		String webappname=getInitParameter("webappname");
		LogManager.log("数据点正常！");
		try {
			Class.forName("com.mysql.jdbc.Driver");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Connection con=null;
		try {
			con=DriverManager.getConnection(dburl,username,password);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(con==null){
			System.err.println("初始化出现问题，无法获得数据库连接，服务器启动无法继续进行，tomcat进程即将结束！");
			System.exit(0);
		}
		LogManager.log("成功得到数据库连接！");
		LogManager.log("开始同步数据！");
		application.setAttribute("dburl", dburl);
		application.setAttribute("username", username);
		application.setAttribute("password", password);
		application.setAttribute("thishost", thishost);
		application.setAttribute("realpath", realpath);
		application.setAttribute("datapath", datapath);
		application.setAttribute("systempi", systempi);
		application.setAttribute("con", con);
		application.setAttribute("webappname", webappname);
		LogManager.log("数据点数据同步完成！");
		LogManager.log("JOJ的tomcat线上系统OnlineJudge启动成功并开始服务！");
	}

}
